<template>
	<view class="content">
		<view class="mp-search-box">
			<input class="ser-input" v-model="deptName" type="text" placeholder="搜索科室">
				<uni-icons class="searchImg" type="search" @click="search"></uni-icons>
			</input>
		</view>
		<scroll-view scroll-y class="left-aside">
			<view v-for="item in flist" :key="item.value" class="f-item b-b" :class="{ active: item.value == currentId }"
				@click="tabtap(item)">{{ item.label }}</view>
		</scroll-view>
		<scroll-view scroll-with-animation scroll-y class="right-aside" @scroll="asideScroll"
			:scroll-top="tabScrollTop">
			<view class="s-list">
				<view class="t-list">
					<view @click="navToList(titem.id)" class="t-item"
						v-for="titem in tlist" :key="titem.id">
						<image :src="titem.avatar"></image>
						<text>{{ titem.nickname }}</text>
					</view>
				</view>
			</view>
		</scroll-view>
	</view>
</template>

<script>
	import {
		listDeptOptions
	} from '../../api/dept/index.js'
	import {
		getUserListByDeptId
	} from '../../api/user/index.js'
	export default {
		data() {
			return {
				tabScrollTop: 0,
				currentId: 0,
				deptName: '',
				flist: [],
				slist: [],
				tlist: []
			};
		},
		onLoad() {
			listDeptOptions().then(response => {
				this.flist = response.data
				this.currentId = this.flist[0].value
				this.tabtap(this.flist[0])
			})
		},
		methods: {
			//搜索
			search(){
				for(let d=0; d<this.flist.length; d++){
					if(this.flist[d].label == this.deptName){
						this.tabtap(this.flist[d])
					}
				}
				if(this.deptName == ''){
					this.tabtap(this.flist[0])
				}
			},
			//一级分类点击
			tabtap(item) {
				getUserListByDeptId(item.value).then(response => {
					const res = response.data.filter(item => item.id !== 2)
					this.slist = res
					this.tlist = res
				})
				if (!this.sizeCalcState) {
					this.calcSize();
				}
				this.currentId = item.value;
				let index = this.slist.findIndex(sitem => sitem.parentId == item.value);
				// this.tabScrollTop = this.slist[index].top;
			},
			//右侧栏滚动
			asideScroll(e) {
				if (!this.sizeCalcState) {
					this.calcSize();
				}
				let scrollTop = e.detail.scrollTop;
				let tabs = this.slist.filter(item => item.top <= scrollTop).reverse();
				if (tabs.length > 0) {
					this.currentId = tabs[0].parentId;
				}
			},
			//计算右侧栏每个tab的高度等信息
			calcSize() {
				let h = 0;
				this.slist.forEach(item => {
					let view = uni.createSelectorQuery().select('#main-' + item.id);
					view.fields({
							size: true
						},
						data => {
							item.top = h;
							h += data.height;
							item.bottom = h;
						}
					).exec();
				});
				this.sizeCalcState = true;
			},
			navToList(id) {
				uni.navigateTo({
					url: `/pages/docter/index?id=${id}`
				});
			}
		},
	};
</script>

<style lang="scss">	
	.searchImg{
		position: absolute;
		left: 15px;
		top: 4px;
	}
	.mp-search-box {
			position: absolute;
			z-index: 9999;
			width: 100%;
			margin-top: 10px;
	
			.ser-input {
				flex: 1;
				height: 56upx;
				line-height: 56upx;
				padding-left: 50px;
				font-size: 28upx;
				color: $font-color-base;
				border-radius: 20px;
				background: #E7E7E7;
			}
		}
		
	page,
	.content {
		height: 100%;
		background-color: #f8f8f8;
	}

	.content {
		flex-direction: row;
		display: flex;
	}

	.left-aside {
		margin-top: 40px;
		flex-shrink: 0;
		width: 200upx;
		height: 635px;
		background-color: #fff;
	}

	.f-item {
		display: flex;
		align-items: center;
		justify-content: center;
		width: 100%;
		height: 100upx;
		font-size: 28upx;
		color: $font-color-base;
		position: relative;

		&.active {
			color: #2BA1DF;
			background: #f8f8f8;

			&:before {
				content: '';
				position: absolute;
				left: 0;
				top: 50%;
				transform: translateY(-50%);
				height: 36upx;
				width: 8upx;
				background-color: #2BA1DF;
				border-radius: 0 4px 4px 0;
				opacity: 0.8;
			}
		}
	}

	.right-aside {
		flex: 1;
		margin-top: 20px;
		overflow: hidden;
		padding-left: 20upx;
		flex-direction: row;
	}

	.s-item {
		display: flex;
		align-items: center;
		height: 70upx;
		padding-top: 8upx;
		font-size: 28upx;
		color: $font-color-dark;
	}

	.t-list {
		display: flex;
		flex-wrap: wrap;
		width: 100%;
		background: #fff;
		padding-top: 10px;

		&:after {
			content: '';
			flex: 99;
			height: 0;
		}
	}

	.t-item {
		flex-shrink: 0;
		display: flex;
		justify-content: center;
		align-items: center;
		flex-direction: column;
		width: 176upx;
		font-size: 26upx;
		color: #666;
		padding-bottom: 20upx;

		image {
			margin-top: 20px;
			width: 140upx;
			height: 140upx;
		}
	}
</style>